
/**
 * For employees only. Displays all information about a spesific project.
 * It links to prepareFileDownload.php and getAttachment.php, and it calls
 * print(..) and getMyProjectComments(..).
 */
function displayOneProject(projectId) {
    $('body > section').empty();
    projectViewed(projectId);
    $.ajax({
        url: 'higface/library/getOneProject.php',
        type : 'POST',
        data: "pid=" + projectId,
        success: function(tmp){
            data = eval ('('+tmp+')');
            if(data.error){
                alert(data.error);
                return;
            }

            /* Create variables with the proper information. */
            var pid = data[0][0], owner = data[0][11], title = data[0][2],
            date = data[0][3], altTitle = data[0][4], shortTitle = data[0][5],
            description = data[0][6], status = data[0][7], collegiumComments
                = data[0][8], advisor1 = data[0][9], advisor2 = data[0][10];

            // If advisor is not yet given, give the variables a default value:
            if(advisor1==null || advisor1=="") advisor1="Ingen veileder gitt.";
            if(advisor2==null || advisor2=="") advisor2="Ingen veileder gitt.";


            /* ---------------------- Append buttons ------------------------ */
            /* Append a hyperlink for downloading the project: */
            addDownloadProjectBtn(pid);
            /* Append a button for downloading the attachment to the project: */
            addDownloadAttachmentBtn(pid);
            /* Append a button for printing the project */
            addPrintProjectBtn(title, altTitle, owner, date,description,status);


            /* --------------------- Append information --------------------- */
            /* Display all general data about the project */
            appendGeneralProjectInfo(title, shortTitle, owner, date);
            /* Display all student-related information about the project */
            appendStudentProjectInfo(advisor1, advisor2);
            /* allows to change status, else display status as normal: */
            appendProjectStatus(status, pid, true);
            /* Display description */
            appendProjectDescription(description);
            /* Display collegiumComments */
            appendProjectCollegiumComments(collegiumComments);
      
            /*
             * Displays a textbox which contains all comments made by the
             * viewing employee for this project, else display an empty textbox
             */
            getMyProjectComments(projectId);
        } // end success
    }); // end $.ajax
} // end function

/* Append a hyperlink for downloading the project: */
function addDownloadProjectBtn(projectId){
    $("body > section").append("<a target='_newfile' \
        class='downloadFileLink' \
        href='higface/employee/library/prepareFileDownload.php?pid="+
        projectId+"'>Last ned Prosjekt</a>");
}

/* Append a button for downloading the attachment to the project: */
function addDownloadAttachmentBtn(projectId){
    var attachment = document.createElement('button');
    attachment.innerHTML = "Last ned Vedlegg";
    attachment.setAttribute("id", "getAttachment");
    attachment.onclick = function() {
        location.href='higface/employee/library/getAttachment.php?id='+ projectId;
    };
    $("body > section").append(attachment);
}

/* Append a button for printing the project */
function addPrintProjectBtn(title, altTitle, owner, date, description, status){
    var printb = document.createElement('button');
    printb.innerHTML = "Skriv ut prosjektet";
    printb.setAttribute("id", "printProjectButton");
    printb.onclick = function() {
        print(title,altTitle, owner, date, description, status);
    };
    $("body > section").append(printb);
}

/*
 * Display all the general information about a project: title, shortTitle,
 * owner and date about the project. 
 */
function appendGeneralProjectInfo(title, shortTitle, owner, date){
    $("body > section").append('<div id="projectInfo">\
        <p id="pT">'+title+'<hr></p>\
        <p id="pST">Kort Tittel: <span id="pShortTitle">'+shortTitle+'</span>\
        <p id="pO">Eier: <span id="pOwner">'+owner+'</span></p>\
        <p id="pD">Opplastet: <span id="pDate">'+date+'</span></p></div>');
}

/* Display all student-related information about the project: advisor1 and
 * advisor 2. TODO: make it so it displays the studentgroup too! */
function appendStudentProjectInfo(advisor1, advisor2){
    $("body > section").append('<div id="projectInfo">\
        <p id="pStG">Gitt til Studentgruppe: <span id="pStudentGroup">????</span></p>\
        <p id="pA1">1. Veileder: <span id="pAdvisor1">'+advisor1+'</span></p>\
        <p id="pA2">2. Veileder: <span id="pAdvisor2">'+advisor2+'</span></p></div>');
}

/* Display the status for the project. If the project already has been reviewed
 * in a meeting, the employee is allowed to change status, else display status 
 * as normal: */
function appendProjectStatus(status, pid, allowed){
    if((status == 'need reworking' || status=='reworked') && allowed==true){
        displayDropDownStatus(status, pid);
    }else{
        $("body > section").append('<div id="projectInfo"><p id="pS">\
        Status: <span id="pStatus">'+status+'</span></p></div>');
    }
}

/* Display a project's description */
function appendProjectDescription(description){
    $("body > section").append('<div id="projectInfo">\
        <p id="pDc">Beskrivelse: <div id="pDescription">'+description+
        '</div></p></div>');
}

/* Display collegiumComments of a project */
function appendProjectCollegiumComments(collegiumComments){
     $("body > section").append('<div id="projectInfo">\
        <p id="pCC">Kommentarer fra ansatte: <div id="pCComments">'
            +collegiumComments+'</div></p></div>');
}

/**
 * Displays a textbox where employees can write personal comments about a
 * project. If the employee has already written a personal comment, then it
 * will be displayed inside the textbox.
 */
function getMyProjectComments(projectId){
    // Create the textarea:
    var commentList = document.createElement('textArea');
    commentList.name = "myComments";
    commentList.setAttribute('class', 'staffcomments');
    commentList.setAttribute('placeholder', 'Skriv ditt notat her');

    // Append Headline, textarea and save-note-button:
    $('body > section').append('<p id="notes">Ditt personlige notat:</p>');
    $('body > section').append(commentList);
    $('body > section').append('<button id="myCommentButton">Lagre notat</button>');

    // update database when myCommentButton was pressed:
    $('#myCommentButton').click(function(){
        updateStaffcomments(projectId, commentList.value);
    });

    /* If the comment made by the employee exists, fetch and display it: */
    $.ajax({
        url: 'higface/employee/library/getMyProjectComment.php',
        type: 'POST',
        data: "pid=" + projectId,
        success: function(tmp){ // the employee has already made a comment:
            data = eval ('('+tmp+')');
            if(data.error){
                alert(data.error);
                return;
            }
            commentList.value = data[2]; // Display the comment in textarea.
        }
    }); // end $.ajax
} // end function


/**
 * For employee only. It updates the employees staffcomments in the database.
 * Is triggered by saving the written comment in getMyProjectComments(..).
 */
function updateStaffcomments(projectId, comment){
    $.ajax({
        url: 'higface/employee/library/updateProjectComment.php',
        type: 'POST',
        data: {
            'pid' : projectId,
            'comm' : comment
        },
        success: function(){    // if the comment was successfully updated:
            if(data.error){
                alert(data.error);
                return;
            }
           message("Notatet er oppdatert", "");
        }
    });// end $.ajax
} // end function
